package com.example.eldersystem.operatedata.service;

import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.mapper.EventMapper;
import com.example.eldersystem.operatedata.model.Event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EventService {

    private EventMapper eventMapper;

    @Autowired
    public void setEventMapper (EventMapper eventMapper) {
        this.eventMapper = eventMapper;
    }

    public int getEventNum(){
        return eventMapper.selectEventNum();
    }

    public DataResponse<String> addEvent(Event event){
        DataResponse<String> dataResponse = new DataResponse<>();

            if(eventMapper.addEvent(event)>0){
                dataResponse.setMsg("添加事件成功");
            }else {
                dataResponse.setCode(151);
                dataResponse.setMsg("添加事件失败");
            }

        return dataResponse;
    }

    

    public DataResponse<List<Event>> selectEvent(){
        DataResponse<List<Event>> dataResponse = new DataResponse<>();
        List<Event> list;
        int total = eventMapper.selectEventNum();
        if(total<=0){
            dataResponse.setCode(10);
            dataResponse.setMsg("查询无结果");
        }else{
            dataResponse.setMsg("查询成功");
            list = eventMapper.selectEvent();
            dataResponse.setData(list);
        }
        return dataResponse;
    }

    public DataResponse<List<Event>> selectEventByName(String name){
        DataResponse<List<Event>> dataResponse = new DataResponse<>();
        List<Event> list;
        int total = eventMapper.selectEventNum();
        if(total<=0){
            dataResponse.setCode(10);
            dataResponse.setMsg("查询无结果");
        }else{
            dataResponse.setMsg("查询成功");
            list = eventMapper.selectEvent();
            for (int i = 0; i < list.size(); i++) {
                Event e = (Event)list.get(i);
                if(!(e.getOldPersonName().equals(name)))
                    list.remove(e);
            }
            dataResponse.setData(list);
            if(list.size()<=0){
                dataResponse.setCode(10);
                dataResponse.setMsg("查询无结果");
                dataResponse.setData(null);
            }
        }
        return dataResponse;
    }

}
